<?php

class Account extends Public_Controller {

	function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		$this->logout();
	}
	
	public function login()
	{
		$this->sSkinFile = 'bare';
		$this->sPageTitle = 'Heathrow Commuter - Library of Assets';
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$this->load->view('platform/'.$this->sPlatform.'/login');
	}
	
	public function forgot_p()
	{
		$this->sSkinFile = 'bare';
		$this->sPageTitle = 'Heathrow Commuter - Library of Assets';
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$sErrorMessage = NULL;
		
		$sEmail = $this->input->post('login_forgot', true, '');
		
		if($sEmail == '')
		{
			$sErrorMessage = 'Please enter your email address';
		}
		else
		{
			$this->load->model('db/users', 'users');
			
			$this->users->user_email = $sEmail;
			$qUser = $this->users->get(1);
			
			if($qUser->num_rows() === 0)
			{
				$sErrorMessage = 'There was no account found with the email address you entered.';
			}
			else
			{
				$sIdent = random_string('unique');
				
				$this->load->model('db/user_forgot_password', 'user_forgot_password');
				$this->user_forgot_password->user_id = $qUser->row(0)->user_id;
				$this->user_forgot_password->ident = $sIdent;
				$this->user_forgot_password->save();
				
				$this->load->library('email');
				
				$this->email->from('your@example.com', 'Library of Assets Site');
				$this->email->to($sEmail);
				
				$sMessage = "Hi".chr(10)."Please go to the following URL to rest your password.".chr(10).chr(10).site_url("account/forgot_p_pre_reset/".$sIdent);
				
				$this->email->subject('Resetting your password');
				$this->email->message($sMessage);
				
				//$this->email->send();
			}
		}
		
		$aData = array(
			'sErrorMessage' => $sErrorMessage, 
			'sEmail' => $sEmail
		);
			
		$this->load->view('platform/'.$this->sPlatform.'/forgot_p_sent', $aData);
	}
	
	public function forgot_p_pre_reset($sIdent = '')
	{
		$this->sSkinFile = 'bare';
		$this->sPageTitle = 'Heathrow Commuter - Library of Assets';
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$this->load->model('db/user_forgot_password', 'user_forgot_password');
		$this->user_forgot_password->ident = $sIdent;
		$this->user_forgot_password->clicked = 0;
		$qUser = $this->user_forgot_password->get(1);
		
		$sErrorMessage = NULL;
		$iUserId = NULL;
		
		if($qUser->num_rows() === 1)
		{
			$iUserId = $qUser->row(0)->user_id;
			
			$this->user_forgot_password->id = $qUser->row(0)->id;
			$this->user_forgot_password->clicked = 1;
			$this->user_forgot_password->save();
		}
		else
		{
			$sErrorMessage = 'There was a problem accessing your details. You may have been arrived here accidentally or clicked through from an old link.';
		}
		
		$aData = array(
			'sErrorMessage' => $sErrorMessage, 
			'iUserId' => $iUserId
		);
			
		$this->load->view('platform/'.$this->sPlatform.'/forgot_p_pre_reset', $aData);
	}
	
	public function forgot_p_reset()
	{
		$this->sSkinFile = 'bare';
		$this->sPageTitle = 'Heathrow Commuter - Library of Assets';
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$sErrorMessage = NULL;
		
		$sNewPassword = $this->input->post('login_new_password', true, '');
		$sNewPasswordCheck = $this->input->post('login_new_password_check', true, '');
		$iUserId = $this->input->post('login_user_id', true, '');
		
		$aData = array(
			'sErrorMessage' => $sErrorMessage, 
			'iUserId' => $iUserId
		);
		
		if($sNewPassword == '' || $sNewPasswordCheck == '' || $sNewPassword != $sNewPasswordCheck)
		{
			$this->load->view('platform/'.$this->sPlatform.'/forgot_p_pre_reset', $aData);
			
			return;
		}
		
		$this->load->model('db/users', 'users');
		$this->users->user_id = $iUserId;
		$this->users->user_passwd = md5($sNewPassword);
		
		$this->users->save();
			
		$this->load->view('platform/'.$this->sPlatform.'/forgot_p_reset', $aData);
	}
	
	public function resend_varifier($sUserId)
	{
		$iUserId = base64_decode($sUserId);
		
		$this->sSkinFile = 'bare';
		$this->sPageTitle = 'Heathrow Commuter - Library of Assets';
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$sErrorMessage = NULL;
		
		$this->load->model('db/users', 'users');
		
		$this->users->user_id = $iUserId;
		
		$qUser = $this->users->get(1);
		
		if($qUser->num_rows() === 1)
		{
			$sVarifier = random_string('unique');
			
			$this->users->user_varified = 0;
			$this->users->user_varifier = $sVarifier;
			
			$this->users->save();
			
			//send confirmation email
			$this->load->library('email');
			
			$this->email->from('your@example.com', 'Library of Assets Site');
			$this->email->to($qUser->row(0)->user_email);
			
			$sMessage = "Hi".chr(10)."Please click the link below to varify your email address.".chr(10).chr(10).site_url("account/varify/".$sVarifier).chr(10).chr(10)."You will need to do this before you can use the site.";
			
			$this->email->subject('Please varify you email address');
			$this->email->message($sMessage);
			
			//$this->email->send();
		}
		else
		{
			$sErrorMessage = 'There was a problem accessing your account details.';
		}
		
		$aData = array(
			'sErrorMessage' => $sErrorMessage
		);
		
		$this->load->view('platform/'.$this->sPlatform.'/resend_varifier', $aData);
	}
	
	public function varify($sVarifier = '')
	{
		$this->sSkinFile = 'bare';
		$this->sPageTitle = 'Heathrow Commuter - Library of Assets';
		
		$this->aPageCss[] = array(
							'combine_css' => true,
							'base_href' => '/media/css',
							'css' => array('generic'),
							'media' => 'all',
							'version' => '1.0'
		);
		
		$sErrorMessage = NULL;
		
		$this->load->model('db/users', 'users');
		$this->users->user_varified = 0;
		$this->users->user_varifier = $sVarifier;
		
		$qUser = $this->users->get(1);
		
		if($qUser->num_rows() === 1)
		{
			$this->users->user_id = $qUser->row(0)->user_id;
			$this->users->user_varified = 1;
			$this->users->user_varifier = NULL;
			
			$this->users->save();
		}
		else
		{
			$sErrorMessage = 'There was a problem accessing your details. You may have been arrived here accidentally or clicked through from an old link.';
		}
		
		$aData = array(
			'sErrorMessage' => $sErrorMessage
		);
		
		$this->load->view('platform/'.$this->sPlatform.'/varified', $aData);
	}
		
	public function logout()
	{
		$this->load->model('user');
		
		$this->user->clear_session();
		
		redirect('/', 'location');
	}
}


?>